package cn.edu.hpu.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.dao.Where;
import cn.edu.hpu.pojo.Deal;
import cn.edu.hpu.pojo.Message;
import cn.edu.hpu.pojo.Task;
import cn.edu.hpu.pojo.Tasktype;
import cn.edu.hpu.pojo.User;
import cn.edu.hpu.service.DealService;
import cn.edu.hpu.service.MessageService;
import cn.edu.hpu.service.TaskService;
import cn.edu.hpu.service.TasktypeService;
import cn.edu.hpu.service.UserService;
import cn.edu.hpu.util.DateUtil;
import cn.edu.hpu.util.Pager;
import cn.edu.hpu.util.TimeUtil;

import com.opensymphony.xwork2.ActionSupport;

public class TaskAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private TaskService taskService;
	private TasktypeService tasktypeService;
	
	private UserService userService;
	private String uid;
	private List<Task> taskList;
	private List<Task> taskList1;
	private Task task;
	private String id;
	private List<Tasktype> tasktypeList;
	private MessageService messageService;
	
	private String did;
	private DealService dealService;
	private Deal deal;
	private Pager<Task> pager;
	private Pager<Task> pager1;
	private int p;
	
	public String execute(){
		
		taskList = taskService.findByProperty("complete", false);
		taskList1 = taskService.findByProperty("complete", true);
		//tasktypeList = tasktypeService.findAll();
		
		return "success";
		
	}
	
	
	public String edit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		task = taskService.findById(id);
		System.out.println(task.getView());
		int date = DateUtil.getDifferDays(task.getStartdate(), task.getEnddate());
		request.setAttribute("date", date);
		tasktypeList = tasktypeService.findAll();
		return "success";
	}
	
	
	public String update(){
		Task t = new Task();
		HttpServletRequest request = ServletActionContext.getRequest();
		String date = request.getParameter("date");
		String ttid = request.getParameter("ttid");
		t.setCreatetime(DateUtil.getTime());
		
		if(date.equals("today")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getTime());
		}else if(date.equals("tomorrow")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(),1));
		}else if(date.equals("thisweek")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(), 7));
		}else if(date.equals("nextweek")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(), 14));	
		}
	System.out.println(task.getView());
		
		User user = userService.findAUser(uid);
		Tasktype tt= tasktypeService.findById(ttid);
		
		
		Message message = new Message();
		if(task.getView()==null){
			String view = "me"+","+uid;
			t.setView(view);
			message.setView(view);
		}else if(task.getView().equals("all")){
			t.setView(task.getView());
			message.setView(task.getView());
		}
		
		t.setComplete(task.isComplete());
		t.setTitle(task.getTitle());
		t.setId(task.getId());
		t.setUser(user);
		t.setTasktype(tt);
		taskService.update(t);
		
		message.setCreatetime(TimeUtil.getTime());
		String title = task.getTitle()+","+"task.action"+","+t.isComplete();
		message.setTitle(title);
		String type = tt.getType()+","+tt.getColor();
		message.setType(type);
		message.setUser(user);
		message.setCtdid(t.getId());
		message.setEnable(true);
		messageService.update(message);
		
		return "success";
	}
	
	
	public String save(){
		System.out.println(task.getView());
		Task t = new Task();
		Message message = new Message();
		HttpServletRequest request = ServletActionContext.getRequest();
		String date = request.getParameter("date");
		String ttid = request.getParameter("ttid");
		if(task.getView()==null){
			String view = "me"+","+uid;
			t.setView(view);
			message.setView(view);
		}else if(task.getView().equals("all")){
			t.setView(task.getView());
			message.setView(task.getView());
		}
		t.setCreatetime(DateUtil.getTime());
		if(date.equals("today")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getTime());
		}else if(date.equals("tomorrow")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(),1));
		}else if(date.equals("thisweek")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(), 7));
		}else if(date.equals("nextweek")){
			t.setStartdate(DateUtil.getTime());
			t.setEnddate(DateUtil.getDate(DateUtil.getTime(), 14));	
		}
		
		User user = userService.findAUser(uid);
		Tasktype tt= tasktypeService.findById(ttid);
		
		t.setTitle(task.getTitle());
		t.setTasktype(tt);
		t.setUser(user);
		System.out.println(did);
		if(did==null){
		
		taskService.save(t);
		
		message.setCreatetime(TimeUtil.getTime());
		String title = task.getTitle()+","+"task.action"+","+t.isComplete();
		message.setTitle(title);
		String type = tt.getType()+","+tt.getColor();
		message.setType(type);
		message.setUser(user);
		message.setCtdid(t.getId());
		message.setEnable(true);
		messageService.save(message);
		
		return "success";
		}else{
			Deal deal = dealService.findById(did);
			t.setDeal(deal);
			taskService.save(t);
			
			message.setCreatetime(TimeUtil.getTime());
			String title = task.getTitle()+","+"task.action"+","+t.isComplete();
			message.setTitle(title);
			String type = tt.getType()+","+tt.getColor();
			message.setType(type);
			message.setUser(user);
			message.setCtdid(t.getId());
			message.setEnable(true);
			messageService.save(message);
			return "error";
			
		}
	}
	
	public String del(){
		taskService.delete(id);
		Message message = messageService.findA(id);
		message.setEnable(false);
		messageService.update(message);
		return "success";
	}
	
	public String add(){
		tasktypeList = tasktypeService.findAll();
		if(did!=null){
			deal = dealService.findById(did);
		}
		
		return "success";
	}
	
	public String change(){
		Task task = taskService.findById(id);
		task.setComplete(true);
		taskService.save(task);
		Message message = messageService.findA(id);
		String title = task.getTitle()+","+"task.action"+","+task.isComplete();
		message.setTitle(title);
		messageService.update(message);
		return "success";
	}
	public String change1(){
		Task task = taskService.findById(id);
		task.setComplete(true);
		taskService.save(task);
		return "success";
	}
	
	//set get

	public TaskService getTaskService() {
		return taskService;
	}
	@Autowired
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}


	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	
	@Autowired
	public void setTasktypeService(TasktypeService tasktypeService) {
		this.tasktypeService = tasktypeService;
	}
	@Autowired
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public TasktypeService getTasktypeService() {
		return tasktypeService;
	}
	public List<Tasktype> getTasktypeList() {
		return tasktypeList;
	}
	public void setTasktypeList(List<Tasktype> tasktypeList) {
		this.tasktypeList = tasktypeList;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}


	public List<Task> getTaskList1() {
		return taskList1;
	}


	public void setTaskList1(List<Task> taskList1) {
		this.taskList1 = taskList1;
	}


	public String getDid() {
		return did;
	}


	public void setDid(String did) {
		this.did = did;
	}


	public DealService getDealService() {
		return dealService;
	}

	@Autowired
	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}


	public Deal getDeal() {
		return deal;
	}


	public void setDeal(Deal deal) {
		this.deal = deal;
	}


	public Pager<Task> getPager() {
		return pager;
	}


	public void setPager(Pager<Task> pager) {
		this.pager = pager;
	}


	public int getP() {
		return p;
	}


	public void setP(int p) {
		this.p = p;
	}


	public Pager<Task> getPager1() {
		return pager1;
	}


	public void setPager1(Pager<Task> pager1) {
		this.pager1 = pager1;
	}



	
	
	
}
